package cn.chendd.api.config;

import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

/**
 * 特定功能设置，不需要@Configuration注解
 *
 * @author chendd
 * @date 2023/1/14 17:01
 */
public class FeatiresFeignConfiguration implements RequestInterceptor {

    /**
     * 自定义日志输出级别
     * @return 日志输出
     */
    @Bean
    public Logger.Level loggerLevel() {
        return Logger.Level.FULL;
    }

    /**
     * 拦截器
     * @param requestTemplate requestTemplate
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //增加parameter参数
        requestTemplate.query("name" , "chendd");
        //增加header参数
        requestTemplate.header("email" , "88911006@qq.com");
        //覆盖url地址，可用于动态获取的url路径，也可以随便写个不存在的地址模拟接口调用失败，验证接口熔断
        ///requestTemplate.target("http://127.0.0.1:8020/");
        //request请求参数获取
        /*ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            if (request != null) {
                requestTemplate.header("contextPath" , request.getContextPath());
            }

        }*/
    }
}
